954D - Fight Against Traffic - CodeForces Solution


dfs and similar graphs shortest paths *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define fi first
#define se second
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
#define yes {cout<<"Yes"<<endl;return;}
#define no {cout<<"No"<<endl;return;}
#define cntbit __builtin_popcountll
#define pii pair<int,int>
#define vii vector<pair<int,int>>
#define all(x) (x).begin(), (x).end()
using namespace std;
const int N = 1010;
const int inf = 1e18;
const int mod = 1e9+7;
void add(int &a,int b){
	a+=b;
	if(a>=mod) a-=mod;
}
int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}
int qmi(int a,int b){
	int res=1;
	for(;b;b>>=1){
		if(b&1) res=res*a%mod;
		a=a*a%mod; 
	}return res;
}
int n,m,s,t; vector<int> e[N];
vector<vector<int>> dis;
void bfs(int s){
	dis[s][s]=0;
	queue<int> q;
	q.push(s);
	while(q.size()){
		int u=q.front();
		q.pop();
		for(auto v:e[u]){
			if(dis[s][v]>dis[s][u]+1){
				q.push(v);
				dis[s][v]=dis[s][u]+1;
			}
		}
	}
}
int edge[N][N];
void solve(){
	cin>>n>>m>>s>>t;
	dis=vector<vector<int>> (n+1,vector<int> (n+1,inf));
	int ans=0;
	while(m--){
		int u,v;cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
		edge[u][v]=1;
		edge[v][u]=1;
	}
	rep(i,1,n+1) bfs(i);
	rep(i,1,n+1) rep(j,i+1,n+1){//won't dec
		if(edge[i][j]) continue;
		int cond1=((dis[s][i]+1+dis[j][t])>=dis[s][t]);
		int cond2=((dis[s][j]+1+dis[i][t])>=dis[s][t]);
		cond1&=cond2;
		if(cond1) ans++;
	}
	cout<<ans<<endl;
	
	
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	cout<<fixed<<setprecision(12);
	int task=1;//cin>>task;
	rep(i,0,task){
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing